
var detailData;
var memberTable;
function getMemberList() {
    if (typeof memberTable !== 'undefined') {
        memberTable.fnPageChange('first');
        return;
    }
    var i = 1;
    memberTable = $("#dataList").DataTable({
        "sAjaxSource": _path + "/member/queryByPage?userType=0",     //服务端
        "sServerMethod": "POST",    //请求方式
        "sAjaxDataProp": "dataList",//后台返回数据名
        "fnServerParams": function (aoData) {               //ajax请求参数
            aoData.push(
                { "name": "fromDate", "value": $("#fromDate").val() },
                { "name": "toDate", "value": $("#toDate").val() },
                { "name": "keyType", "value": $("#keyType").val() },
                { "name": "keywords", "value": $("#keywords").val() }
            );
        },
        "fnServerData": function(sUrl, aoData, fnCallback) {
            var data = new Array();
            for(var i in aoData)
                data.push(aoData[i].name + '=' + aoData[i].value);
            $.ajax( {
                "type": "POST",
                "url": sUrl,
                "dataType": "json",
                "data": data.join('&'),
                "success": function(resp) {
                    detailData = resp.dataList;
                    fnCallback(resp);
                }
            });
        },
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            },
            { "sTitle": "用户名", "mData": "userName", "sWidth": "10%" },
            { "sTitle": "手机号", "mData": "mobile", "sWidth": "10%" },
            { "sTitle": "状态", "sWidth": "6%",
                "mData": function (data) {
                    var status = '';
                    switch (data.status) {
                        case '0':
                            status = '正常';
                            break;
                        case '1':
                            status = '禁用';
                            break;
                        default:
                            status = '未知';
                            break;
                    }
                    return status;
                }},
            { "sTitle": "认证", "sWidth": "7%",
                "mData": function (data) {
                    var status = '';
                    switch (data.approve) {
                        case '0':
                            status = '未认证';
                            break;
                        case '1':
                            status = '已认证';
                            break;
                        case '2':
                            status = '认证失败';
                            break;
                        case '3':
                            status = '<font color="red">等待认证</font>';
                            break;
                        default:
                            status = '';
                            break;
                    }
                    return status;
                }},
            { "sTitle": "注册时间", "mData": "createTime", "sWidth": "8%" },
            { "sTitle": "余额", "mData": "money", "sWidth": "8%" },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                    var html = new Array();
                    html.push('<a href="javascript:modal(\'' + data.id + '\', \'' + data.userName + '\', \'' + data.money + '\')" class="btn default btn-xs red">');
                    html.push('<i class="fa fa-gift" title="充值"></i> </a>');
                    html.push('<a href="' + _path + '/member/edit/' + data.id + '"  class="btn default btn-xs purple">');
                    html.push('<i class="fa fa-edit" title="编辑"></i> </a>');
                    html.push('<a href="javascript:oper(6,\'' + data.id + '\');" id="btnA" class="btn  btn-xs green">');
                    html.push('<i class="fa fa-check-square-o" title="审核通过"></i> </a>');
                    html.push('<a href="javascript:oper(7,\'' + data.id + '\');" id="btnB" class="btn  btn-xs blue">');
                    html.push('<i class="fa fa-times-circle-o" title="审核不通过"></i> </a>');
                    html.push('<a href="'+_path+'/info/send?userId='+data.id+'" class="btn  btn-xs green">');
                    html.push('<i class="fa fa-bullhorn" title="发送消息"></i> </a>');
                    html.push('<a href="javascript:oper(4,\'' + data.id + '\');" id="btnC" class="btn  btn-xs red">');
                    html.push('<i class="fa fa-minus-square" title="禁用"></i> </a>');
                    html.push('<a href="javascript:oper(5,\'' + data.id + '\');" id="btnE"  class="btn  btn-xs yellow">');
                    html.push('<i class="fa fa-reply" title="恢复"></i> </a>');
                    html.push('<a href="javascript:delUser(\'' + data.id + '\');" id="btnDel" class="btn  btn-xs red">');
                    html.push('&nbsp;<i class="fa fa-trash-o" title="删除"></i> </a>');
                    return html.join('');
                }
            }
        ]
    });
}
function delUser(id) {
    if (confirm("确认删除选中的记录？")) {
        oper(3, id);
    }
}
function conver(str) {
    return str ? str : '';
}
function modal(id, name, money) {
    $('#panel').on('shown.bs.modal', function () {
        $("#memberid").val(id);
        $("#memberName").text(name);
        $("#money").val(money);
    }).modal('show');
}
/**
 * type: 1增，2刷新，3删除
 */
function oper(type, id) {
    App.ajax(_path + "/member/oper", "id=" + id + "&type=" + type, function (data) {
        switch (data.statusCode) {
            case 200 :
                alert("操作成功！");
                break;
            case 500 :
                alert(data.info);
                break;
            default :
                alert("操作失败！");
                break;
        }
        window.location.href = _path + "/member/list?userType=0";
    });
}

function fnFormatDetails(oTable, nTr) {
    var aData = oTable.fnGetData(nTr);
    var html = new Array();
    var data = detailData[nTr._DT_RowIndex]
    html.push('<form id="subTable">');
    html.push('<table width="100%" id="sample_editable_1">');
    html.push('<tr>');
    html.push('<td>真实姓名:</td>');
    html.push('<td>' + conver(data.realname) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>性别:</td>');
    html.push('<td>' + conver(data.sex) + '</td>');
    html.push('<td>生日:</td>');
    html.push('<td>' + conver(data.birthday) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>个性签名:</td>');
    html.push('<td>' + conver(data.signature) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>所属省:</td>');
    html.push('<td>' + conver(data.businessAddress) + '</td>');
    html.push('</tr><tr>');
    html.push('<td>积分:</td>');
    html.push('<td>' + conver(data.score) + '</td>');
    html.push('<td>S币:</td>');
    html.push('<td>' + conver(data.money) + '</td>');
    html.push('</tr>');
    html.push('</table>');
    html.push('</form>');
    return html.join('');
}